﻿/*
This file is under Microsoft Public License (MS-PL)
Copyright Yohann MARTEL.  All rights reserved.
Codeplex: http://amf.codeplex.com
AMF Website: http://www.ymartel.com/aspnet-mobile-framework/
*/

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Mobile.WebControls
{
    public class MobileResourcesManager : BaseControl
    {
        #region Properties
        [Category("Functionality")]
        public LinkRessourcesHost LinkRessourcesHost{ get; set; }

        [Category("Links")]
        public string JqueryJavascript { get; set; }
        [Category("Links")]
        public string JqueryMobileJavascript { get; set; }
        [Category("Links")]
        public string JqueryMobileStylesheet { get; set; }
        #endregion

        #region Methods
        protected override void CreateChildControls()
        {
            AddStyleSheet();
            AddJavascript();
            base.CreateChildControls();
        }

        private string GetStylesheetUrl()
        {
            string url = JqueryMobileStylesheet;
            switch (LinkRessourcesHost)
            {
                case LinkRessourcesHost.SelfHosting:
                    url = Page.ClientScript.GetWebResourceUrl(GetType(), MetadataResources.JqueryMobileCssResourceName);
                    break;
                case LinkRessourcesHost.Jquery:
                    url = MetadataResources.JqueryMobileCss;
                    break;
                    /* Deprecated for the moment
                case LinkRessourcesHost.Microsoft:
                    url = MetadataResources.MicrosoftJqueryMobileCss;
                    break;
                     */ 
                case LinkRessourcesHost.Custom:
                    if (String.IsNullOrEmpty(url))
                    {
                        url = MetadataResources.JqueryMobileCss;
                    }
                    break;
            }
            return url;
        }

        private string GetJqueryJsUrl()
        {
            string url = JqueryJavascript;
            switch (LinkRessourcesHost)
            {
                case LinkRessourcesHost.SelfHosting:
                    url = Page.ClientScript.GetWebResourceUrl(GetType(), MetadataResources.JqueryJsResourceName);
                    break;
                case LinkRessourcesHost.Jquery:
                    url = MetadataResources.JqueryJs;
                    break;
                    /* Deprecated for the moment
                case LinkRessourcesHost.Microsoft:
                    url = MetadataResources.MicrosoftJqueryJs;
                    break;
                     */
                case LinkRessourcesHost.Custom:
                    if (String.IsNullOrEmpty(url))
                    {
                        url = MetadataResources.JqueryJs;
                    }
                    break;
            }
            return url;
        }

        private string GetJqueryMobileJsUrl()
        {
            string url = JqueryMobileJavascript;
            switch (LinkRessourcesHost)
            {
                case LinkRessourcesHost.SelfHosting:
                    url = Page.ClientScript.GetWebResourceUrl(GetType(), MetadataResources.JqueryMobileJsResourceName);
                    break;
                case LinkRessourcesHost.Jquery:
                    url = MetadataResources.JqueryMobileJs;
                    break;
                    /* Deprecated for the moment
                case LinkRessourcesHost.Microsoft:
                    url = MetadataResources.MicrosoftJqueryMobileJs;
                    break;
                     */
                case LinkRessourcesHost.Custom:
                    if (String.IsNullOrEmpty(url))
                    {
                        url = MetadataResources.JqueryMobileJs;
                    }
                    break;
            }
            return url;
        }

        #endregion

        #region Render Methods
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
        }

        private void AddStyleSheet()
        {
            HtmlLink hlink = new HtmlLink();
            hlink.Href = GetStylesheetUrl();
            hlink.Attributes["rel"] = "stylesheet";
            hlink.Attributes["text"] = MetadataType.Css;
            this.Controls.Add(hlink);
        }

        private void AddJavascript()
        {
            HtmlScript jquery = new HtmlScript();
            jquery.Source = GetJqueryJsUrl();
            this.Controls.Add(jquery);
            HtmlScript jqueryMobile = new HtmlScript();
            jqueryMobile.Source = GetJqueryMobileJsUrl();
            this.Controls.Add(jqueryMobile);
        }
        #endregion
    }
}
